import 'package:inmotion_pro/config/r.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/routes/app_routes.dart';
import 'package:inmotion_pro/service/global_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'controller.dart';

class RootView extends BaseGetView<RootController> {
  RootView({super.key});
  @override
  RootController get controller => Get.find(tag: AppRoutes.ROOT);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Obx(
        // 使用 IndexedStack 来保持页面状态
        () => IndexedStack(
          index: controller.state.currentIndex.value,
          children: controller.state.pages,
        ),
      ),
      bottomNavigationBar: Obx(
        () => AnimatedBottomNavigationBar(
          backgroundColor: GrayColor.gray1,
          currentIndex: controller.state.currentIndex.value,
          onTap: (index) => controller.currentPageChanged(index),
          items: [
            BottomNavigationBarItem(
              icon: _buildImage(R.tabbarVehicleIconN),
              activeIcon: _buildImage(R.tabbarVehicleIconS),
              label: context.s.vehicle,
            ),
            BottomNavigationBarItem(
              icon: _buildImage(R.tabbarShoppingN),
              activeIcon: _buildImage(R.tabbarShoppingS),
              label: context.s.shopping,
            ),
            BottomNavigationBarItem(
              icon: _buildImage(R.tabbarMineN),
              activeIcon: _buildImage(R.tabbarMineS),
              label: context.s.mine,
            ),
          ],
          badgeCounts: GlobalService.to.badgeCounts,
          showBadges: GlobalService.to.showBadges,
          selectedTextColor: Colors.white,
          unselectedTextColor: Colors.white.withOpacity(0.6),
          animationDuration: const Duration(milliseconds: 400),
          selectedTextStyle: const TextStyle(
            fontSize: 12,
            color: Colors.white,
            fontWeight: FontWeight.w500,
          ),
          unselectedTextStyle: TextStyle(
            fontSize: 12,
            color: Colors.white.withOpacity(0.6),
            fontWeight: FontWeight.w400,
          ),
        ),
      ),
    );
  }

  Widget _buildImage(String imagePath) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 4), // 调整图标下方间距
      child: Image.asset(
        imagePath,
        width: 20.w,
        fit: BoxFit.fitWidth,
      ),
    );
  }
}
